package com.android.svmc.app.mobile.activity;

import luyenlv.pro.screen.base.BaseActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.svmc.app.mobile.controller.CheckNewRecordService;
import com.android.svmc.app.mobile.util.CommonDefine;
import com.android.svmc.app.mobile.util.SettingUtils;
import com.android.svmc.app.mobile.view.CustomDialog;
import com.android.svmc.app.mobile.view.FontTextView;

public class SettingMainActivity extends BaseActivity implements CommonDefine,
		OnClickListener {
	private LinearLayout ll_rotation;
	private LinearLayout ll_notification;
	private LinearLayout ll_offline_mode;
	private LinearLayout ll_account_name;
	private LinearLayout ll_diary;

	private TextView title;
	public static TextView txt_account_name;
	private TextView txt_rotation_title;
	private TextView txt_notification_title;
	private TextView txt_offline_mode_title;
	private TextView txt_account_name_title;
	private TextView txt_sync_diary_title;

	public static CheckBox cb_rotation;
	private CheckBox cb_notification;
	private CheckBox cb_offline_mode;
	private CheckBox cb_sysnc_diary;

	private Button about_app;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(getLayout(R.array.setting_layout));
		initUI();
		setClickListen();
		setCheckBoxListener();
	}

	private void setClickListen() {
		ll_rotation.setOnClickListener(this);
		ll_notification.setOnClickListener(this);
		ll_offline_mode.setOnClickListener(this);
		ll_account_name.setOnClickListener(this);
		ll_diary.setOnClickListener(this);
		txt_account_name.setOnClickListener(this);
		about_app.setOnClickListener(this);
	}

	private void setCheckBoxListener() {
		cb_rotation.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					if (SettingUtils.getbooleanPreferences(
							SettingMainActivity.this, IS_ROTATION_SCREEN) == false) {
						CustomDialog confirm = new CustomDialog(
								getDialogContext());
						confirm.setDialogType(
								DIALOG_WARING_ROTATION,
								SettingMainActivity.this
										.getString(R.string.connent_dialog_waring));
						confirm.show();
					}
				} else {
					SettingUtils
							.savebooleanPreferences(SettingMainActivity.this,
									IS_ROTATION_SCREEN, false);
				}

			}
		});
		cb_notification
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							SettingUtils.savebooleanPreferences(
									SettingMainActivity.this, IS_NOTIFICATION,
									true);
							Intent intent = new Intent(
									SettingMainActivity.this,
									CheckNewRecordService.class);
							if (SettingUtils.getbooleanPreferences(
									SettingMainActivity.this,
									IS_STARTED_SERVICE_NOTIFICATION) == false) {
								startService(intent);
							}
						} else {
							SettingUtils.savebooleanPreferences(
									SettingMainActivity.this, IS_NOTIFICATION,
									false);
							Intent intent = new Intent(
									SettingMainActivity.this,
									CheckNewRecordService.class);
							stopService(intent);
						}

					}
				});
		cb_offline_mode
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							SettingUtils.savebooleanPreferences(
									SettingMainActivity.this, IS_OFFLINE_MODE,
									true);
						} else {
							SettingUtils.savebooleanPreferences(
									SettingMainActivity.this, IS_OFFLINE_MODE,
									false);
						}

					}
				});
		cb_sysnc_diary
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							SettingUtils.savebooleanPreferences(
									SettingMainActivity.this, IS_SYNC_DIARY,
									true);
						} else {
							SettingUtils.savebooleanPreferences(
									SettingMainActivity.this, IS_SYNC_DIARY,
									false);
						}

					}
				});
	}

	private void initUI() {
		ll_rotation = (LinearLayout) findViewById(R.id.setting_ll_rotation);
		ll_notification = (LinearLayout) findViewById(R.id.setting_ll_notification);
		ll_offline_mode = (LinearLayout) findViewById(R.id.setting_ll_offline_mode);
		ll_account_name = (LinearLayout) findViewById(R.id.setting_ll_account_name);
		ll_diary = (LinearLayout) findViewById(R.id.setting_ll_diary);

		title = (TextView) findViewById(R.id.setting_txt_title);
		txt_rotation_title = (TextView) findViewById(R.id.setting_txt_rotation_title);
		txt_notification_title = (TextView) findViewById(R.id.setting_txt_notification_title);
		txt_offline_mode_title = (TextView) findViewById(R.id.setting_txt_offline_mode_title);
		txt_account_name_title = (TextView) findViewById(R.id.setting_txt_account_name_title);
		txt_sync_diary_title = (TextView) findViewById(R.id.setting_txt_sync_diary_title);
		txt_account_name = (TextView) findViewById(R.id.setting_txt_name_display);

		cb_rotation = (CheckBox) findViewById(R.id.setting_cb_rotation_screen);
		cb_notification = (CheckBox) findViewById(R.id.setting_cb_notification);
		cb_offline_mode = (CheckBox) findViewById(R.id.setting_cb_offline_mode);
		cb_sysnc_diary = (CheckBox) findViewById(R.id.setting_cb_sync_diary);

		about_app = (Button) findViewById(R.id.setting_btn_about_application);

		FontTextView.setfontHelveticaBold(title, this);
		FontTextView.setfontMyriadPro_semiBold(txt_rotation_title, this);
		FontTextView.setfontMyriadPro_semiBold(txt_notification_title, this);
		FontTextView.setfontMyriadPro_semiBold(txt_offline_mode_title, this);
		FontTextView.setfontMyriadPro_semiBold(txt_offline_mode_title, this);
		FontTextView.setfontMyriadPro_semiBold(txt_account_name_title, this);
		FontTextView.setfontMyriadPro_semiBold(txt_sync_diary_title, this);

	}

	private void settextUI() {
		title.setText(getString(R.string.tab_setting));
		String name = SettingUtils.getStringPreferences(this,
				ACCOUNT_NAME_DISPLAY);
		if (name.length() >= 10) {
			txt_account_name.setText(name.subSequence(0, 10) + "...");
		} else {
			txt_account_name.setText(name);
		}

	}

	private void setValueBooleanSetting() {
		cb_rotation.setChecked(SettingUtils.getbooleanPreferences(this,
				IS_ROTATION_SCREEN));
		cb_offline_mode.setChecked(SettingUtils.getbooleanPreferences(this,
				IS_OFFLINE_MODE));
		cb_notification.setChecked(SettingUtils.getbooleanPreferences(this,
				IS_NOTIFICATION));
		cb_sysnc_diary.setChecked(SettingUtils.getbooleanPreferences(this,
				IS_SYNC_DIARY));

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		settextUI();
		setValueBooleanSetting();
	}

	@Override
	public void clearMemory() {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.setting_ll_rotation:
			if (cb_rotation.isChecked()) {
				cb_rotation.setChecked(false);
				SettingUtils.savebooleanPreferences(this, IS_ROTATION_SCREEN,
						false);
			} else {
				cb_rotation.setChecked(true);
				CustomDialog confirm = new CustomDialog(getDialogContext());
				confirm.setDialogType(DIALOG_WARING_ROTATION,
						this.getString(R.string.connent_dialog_waring));
				confirm.show();

			}
			break;
		case R.id.setting_ll_notification:
			if (cb_notification.isChecked()) {
				cb_notification.setChecked(false);
				SettingUtils.savebooleanPreferences(this, IS_NOTIFICATION,
						false);
				Intent intent = new Intent(this, CheckNewRecordService.class);
				stopService(intent);
			} else {
				cb_notification.setChecked(true);
				SettingUtils
						.savebooleanPreferences(this, IS_NOTIFICATION, true);
				Intent intent = new Intent(this, CheckNewRecordService.class);
				if (SettingUtils.getbooleanPreferences(this,
						IS_STARTED_SERVICE_NOTIFICATION) == false) {
					startService(intent);
				}
			}
			break;
		case R.id.setting_ll_offline_mode:
			if (cb_offline_mode.isChecked()) {
				cb_offline_mode.setChecked(false);
				SettingUtils.savebooleanPreferences(this, IS_OFFLINE_MODE,
						false);
			} else {
				cb_offline_mode.setChecked(true);
				SettingUtils
						.savebooleanPreferences(this, IS_OFFLINE_MODE, true);
			}
			break;
		case R.id.setting_ll_account_name:
		case R.id.setting_txt_name_display:
			CustomDialog account_name_dialog = new CustomDialog(
					getDialogContext());
			account_name_dialog.setDialogType(DIALOG_SUBMIT_NAME_COMMENT, "");
			account_name_dialog.show();
			break;
		case R.id.setting_ll_diary:
			if (cb_sysnc_diary.isChecked()) {
				cb_sysnc_diary.setChecked(false);
				SettingUtils.savebooleanPreferences(this, IS_SYNC_DIARY, false);
			} else {
				cb_sysnc_diary.setChecked(true);
				SettingUtils.savebooleanPreferences(this, IS_SYNC_DIARY, true);
			}
			break;
		case R.id.setting_btn_about_application:
			Toast.makeText(this, "thông tin phần mềm", 0).show();

			break;
		default:
			break;
		}

	}

	/**
	 * Get context in groupActivity.Use for show dialog
	 * 
	 * @return : context
	 */
	private Context getDialogContext() {
		Context mcontext;
		if (getParent() != null)
			mcontext = getParent();
		else
			mcontext = this;
		return mcontext;
	}

}
